﻿

using DynamicViewFormApp.Component;
using DynamicViewFormApp.Entity;
using DynamicViewFormApp.Enum;
using DynamicViewFormApp.Setting;
using DynamicViewFormApp.Util;
using DynamicViewFormLibrary.Utils;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;

namespace DynamicViewFormApp.SubForm
{
    public partial class StartForm : Form
    {
        public StartForm()
        {
            InitializeComponent();
        }

        private void btnStart_Paint(object sender, PaintEventArgs e)
        {
            Draw(e.ClipRectangle, e.Graphics, 5, false, Color.FromArgb(9, 186, 7), Color.FromArgb(9, 186, 7));
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.DrawString("开始制作", new Font("微软雅黑", 12, FontStyle.Regular), new SolidBrush(Color.White), new PointF(45, 12));
        }

        private void Draw(Rectangle rectangle, Graphics g, int _radius, bool cusp, Color begin_color, Color end_color)
        {
            int span = 2;
            //抗锯齿
            g.SmoothingMode = SmoothingMode.AntiAlias;
            //渐变填充
            LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush(rectangle, begin_color, end_color, LinearGradientMode.Vertical);
            //画尖角
            if (cusp)
            {
                span = 10;
                PointF p1 = new PointF(rectangle.Width - 12, rectangle.Y + 10);
                PointF p2 = new PointF(rectangle.Width - 12, rectangle.Y + 30);
                PointF p3 = new PointF(rectangle.Width, rectangle.Y + 20);
                PointF[] ptsArray = { p1, p2, p3 };
                g.FillPolygon(myLinearGradientBrush, ptsArray);
            }
            //填充
            g.FillPath(myLinearGradientBrush, DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - span, rectangle.Height - 1, _radius));
        }
        public static GraphicsPath DrawRoundRect(int x, int y, int width, int height, int radius)
        {
            //四边圆角
            GraphicsPath gp = new GraphicsPath();
            gp.AddArc(x, y, radius, radius, 180, 90);
            gp.AddArc(width - radius, y, radius, radius, 270, 90);
            gp.AddArc(width - radius, height - radius, radius, radius, 0, 90);
            gp.AddArc(x, height - radius, radius, radius, 90, 90);
            gp.CloseAllFigures();
            return gp;
        }

        private void btnStart_Click(object sender, System.EventArgs e)
        {
            this.Opacity = 0;
            MainForm mainForm = (MainForm)this.Owner;//获取拥有此窗体的窗体
            mainForm.ShowNewForm(this);
        }

        private void StartForm_Load(object sender, System.EventArgs e)
        {
            richTextBox1.Rtf = RtfContent.Documentation;
            DisposeHistoryFileList();
        }

        private void DisposeHistoryFileList()
        {
            string filepath = FilePathUtil.RTFWorkSpaceRootPath();
            //显示指定路径下的文件夹
            DirectoryInfo dir = new DirectoryInfo(filepath);
            DirectoryInfo[] dirs = dir.GetDirectories();
            Array.Sort(dirs, new SortDirectory(SortOption.CreationTime));
            foreach (DirectoryInfo d in dirs)
            {
                TreeNode tNode = new TreeNode(d.Name);
                tNode.ImageIndex = IconIndexes.Folder;
                tNode.SelectedImageIndex = IconIndexes.Folder;
                //显示指定路径下的文件
                FileInfo[] files = d.GetFiles("*.rtfs*");
                Array.Sort(files, new SortFile(SortOption.CreationTime));
                foreach (FileInfo f in files)
                {
                    TreeNode cNode = new TreeNode(f.Name);
                    cNode.ImageIndex = IconIndexes.Files;
                    cNode.SelectedImageIndex = IconIndexes.Files;
                    cNode.Tag = new FileEntity(f.Name, f.FullName);
                    tNode.Nodes.Add(cNode);
                }
                if (tNode.Nodes.Count != 0)//隐藏空目录
                {
                    treeViewHistoryFile.Nodes.Add(tNode);
                }

            }
            this.treeViewHistoryFile.ExpandAll();
            this.treeViewHistoryFile.NodeMouseDoubleClick += new TreeNodeMouseClickEventHandler(this.treeView_NodeMouseClick);
        }

        private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            treeViewHistoryFile.SelectedNode = e.Node;
            if (e.Node.Tag != null)
            {
                try
                {
                    this.Opacity = 0;
                    FileEntity file = (FileEntity)e.Node.Tag;

                    string name = Path.GetFileNameWithoutExtension(file.FilePath);//文件名没有扩展名
                    string paperName = name.Split('_')[0];
                    string paperType = name.Split('_')[1];

                    MainForm mainForm = (MainForm)this.Owner;//获取拥有此窗体的窗体
                    mainForm.LoadWorkSpaceFile(paperName, paperType, file.FilePath);
                    this.Close();
                }
                catch (Exception)
                {
                    MessageBox.Show("文件无效");
                    this.Opacity = 100;
                }
            }
        }
    }
}
